package com.jic.market.controller.manage;

import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.market.common.util.BeanUtil;
import com.jic.market.entity.ProductBargain;
import com.jic.market.request.ProductBargainRequest;
import com.jic.market.response.ProductBargainResponse;
import com.jic.market.service.ProductBargainService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Api(tags = {"砍价活动Controller"})
@Slf4j
@RestController
@RequestMapping("/bargainyProduct")
public class ProductBargainController {

    @Autowired
    private ProductBargainService productBargainService;
    /**
     * 分页查询
     * @param form
     * @param form
     * @return
     */
    @PostMapping("/queryPage")
    @ResponseBody
    @ApiOperation(value = "分页查询")
    public RestResult<PageResult<ProductBargainResponse>> list(@RequestBody ProductBargainRequest form) {
        log.info("开始获取所有列表=========》,{}", form.toString());
        Page page = new Page();
        page.setLimit(form.getLimit());
        page.setStart(form.getStart());
        ProductBargain ProductBargain = BeanUtil.copy(form, ProductBargain.class);
        ProductBargain.setDeleteFlag(0L);
        PageResult<ProductBargain> result = productBargainService.queryPage(ProductBargain, page);
        List<ProductBargainResponse> ProductBargainResponseList = result.getRows().stream().map(temp -> {
            ProductBargainResponse ProductBargainResponse = BeanUtil.copy(temp, ProductBargainResponse.class);
            return ProductBargainResponse;
        }).filter(item -> item != null).collect(Collectors.toList());
        PageResult<ProductBargainResponse> pageResult = new PageResult<>();
        pageResult.setRows(ProductBargainResponseList);
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);
    }

    @PostMapping("/insertProductBargain")
    @ResponseBody
    @ApiOperation(value = "insertProductBargain")
    public RestResult insertProductBargain(@RequestBody ProductBargainRequest form) {
        ProductBargain ProductBargain = BeanUtil.copy(form, ProductBargain.class);
        return  RestResult.success(productBargainService.insert(ProductBargain));
    }

    @PostMapping("/updateProductBargain")
    @ResponseBody
    @ApiOperation(value = "updateProductBargain")
    public RestResult updateProductBargain(@RequestBody ProductBargainRequest form) {
        ProductBargain ProductBargain = BeanUtil.copy(form, ProductBargain.class);
        return productBargainService.updateProductBargain(ProductBargain);
    }

    @PostMapping("/deleteProductBargain")
    @ResponseBody
    @ApiOperation(value = "deleteProductBargain")
    public RestResult deleteProductBargain(@RequestBody ProductBargainRequest form) {
        ProductBargain ProductBargain = BeanUtil.copy(form, ProductBargain.class);
        return productBargainService.deleteProductBargain(ProductBargain);
    }

}
